// DrawText.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include "..\\..\\include\\JHComm.h"

#define COMM_SERIALPORT  1


int _tmain(int argc, _TCHAR* argv[])
{
	HINSTANCE hInstComm, hInstZk;
	HANDLE hdl;
	HANDLE pargstruct;
	HANDLE pjhtransportlayer;
	int ret;
	JHZRESULT res;

	typedef JHZRESULT (JHZAPI* PJHCREATEINST)(HANDLE *, unsigned short);
	PJHCREATEINST pJHCreateInst;

	typedef JHZRESULT (JHZAPI* PJHDELETEINST)(HANDLE);
	PJHDELETEINST pJHDeleteInst;

	typedef JHZRESULT (JHZAPI* PJHCOMMINIT)();
	PJHCOMMINIT pJHCommInit;

	typedef JHZRESULT (JHZAPI* PJHCOMMDEINIT)();
	PJHCOMMDEINIT pJHCommDeinit;

	typedef JHZRESULT (JHZAPI* PJHCOMMGETTRANSPORTLAYER)(HANDLE *, int);
	PJHCOMMGETTRANSPORTLAYER pJHCommGetTransportlayer;

#if COMM_SERIALPORT
	typedef JHZRESULT (JHZAPI* PJHCOMMGETGETSERIALPORTARGSTRUCT)(HANDLE *, const char *, unsigned long, unsigned char, unsigned char, unsigned char, unsigned long);
	PJHCOMMGETGETSERIALPORTARGSTRUCT pJHCommGetSerialportArgStruct;
#else
	typedef JHZRESULT (JHZAPI* PJHCOMMGETGETNETARGSTRUCT)(HANDLE *, const char *, unsigned short, unsigned long);
	PJHCOMMGETGETNETARGSTRUCT pJHCommGetNetArgStruct;
#endif

	typedef JHZRESULT (JHZAPI* PJHCOMMRELEASEARGSTRUCT)(HANDLE);
	PJHCOMMRELEASEARGSTRUCT pJHCommReleaseArgStruct;

	typedef JHZRESULT (JHZAPI* PJHMOUNTTRANSPORTLAYER)(HANDLE, LPJHTRANSPORTLAYER, const void *);
	PJHMOUNTTRANSPORTLAYER pJHMountTransportLayer;

	typedef JHZRESULT (JHZAPI* PJHDRAWTEXT)(HANDLE, short, short, unsigned short, unsigned short, unsigned long, const char *);
	PJHDRAWTEXT pJHDrawText;

	/* Load jinhancomm.dll resources*/
	hInstComm = ::LoadLibraryA("..\\..\\dll\\Windows\\jinhancomm.dll");
	if(NULL == hInstComm)
	{
		printf("Failure to load resources jinhancomm.dll!\n");
		ret = -1;
		goto ERROR_OPERATION;
	}

	/* Load jinhanzk.dll resources*/
	hInstZk = ::LoadLibraryA("..\\..\\dll\\Windows\\jinhanzk.dll");
	if(NULL == hInstZk)                           
	{
		printf("Failure to load resources jinhanzk.dll!\n");
		ret = -1;
		goto ERROR_OPERATION;
	}

	/* Create instance */
	pJHCreateInst= (PJHCREATEINST)GetProcAddress(hInstZk, "JHCreateInst");
	if(NULL == pJHCreateInst)
	{
		printf("Failure to load JHCreateInst address!\n");
		ret = -1;
		goto ERROR_OPERATION;
	}

	res = pJHCreateInst(&hdl, 0);
	if(JR_OK != res)
	{
		printf("\nFailure to pJHCreateInst!\n");
		ret = -1;
		goto ERROR_OPERATION;
	}

	/* Communication mode and parameters setting */
	pJHCommInit = (PJHCOMMINIT)GetProcAddress(hInstComm, "JHCommInit");
	if(NULL == pJHCommInit)
	{
		printf("Failure to load pJHCommInit address!\n");
		ret = -1;
		goto ERROR_OPERATION;
	}

	res = pJHCommInit();
	if(JR_OK != res)
	{
		printf("\nFailure to pJHCommInit!\n");
		ret = -1;
		goto ERROR_OPERATION;
	}

	pJHCommGetTransportlayer = (PJHCOMMGETTRANSPORTLAYER)GetProcAddress(hInstComm, "JHCommGetTransportlayer");
	if(NULL == pJHCommGetTransportlayer)
	{
		printf("Failure to load JHCommGetTransportlayer address!\n");
		ret = -1;
		goto ERROR_OPERATION;
	}

#if COMM_SERIALPORT
	res = pJHCommGetTransportlayer(&pjhtransportlayer, DEV_SERIALPORT);
	if(JR_OK != res)
	{
		printf("\nFailure to pJHCommGetTransportlayer!\n");
		ret = -1;
		goto ERROR_OPERATION;
	}

	pJHCommGetSerialportArgStruct = (PJHCOMMGETGETSERIALPORTARGSTRUCT)GetProcAddress(hInstComm, "JHCommGetSerialportArgStruct");
	if(NULL == pJHCommGetSerialportArgStruct)
	{
		printf("Failure to load pJHCommGetSerialportArgStruct address!\n");
		ret = -1;
		goto ERROR_OPERATION;
	}

	res = pJHCommGetSerialportArgStruct(&pargstruct, "COM1", 115200, 8, 0, 0, 5000);
	if(JR_OK != res)
	{
		printf("\nFailure to pJHCommGetSerialportArgStruct!\n");
		ret = -1;
		goto ERROR_OPERATION;
	}
#else
	res = pJHCommGetTransportlayer(&pjhtransportlayer, DEV_TCP);
	if(JR_OK != res)
	{
		printf("\nFailure to pJHCommGetTransportlayer!\n");
		ret = -1;
		goto ERROR_OPERATION;
	}

	pJHCommGetNetArgStruct = (PJHCOMMGETGETNETARGSTRUCT)GetProcAddress(hInstComm, "JHCommGetNetArgStruct");
	if(NULL == pJHCommGetNetArgStruct)
	{
		printf("Failure to load pJHCommGetNetArgStruct address!\n");
		ret = -1;
		goto ERROR_OPERATION;
	}

	res = pJHCommGetNetArgStruct(&pargstruct, "192.168.1.3", 30000, 5000);
	if(JR_OK != res)
	{
		printf("\nFailure to pJHCommGetNetArgStruct!\n");
		ret = -1;
		goto ERROR_OPERATION;
	}
#endif
	
	pJHCommReleaseArgStruct = (PJHCOMMRELEASEARGSTRUCT)GetProcAddress(hInstComm, "JHCommReleaseArgStruct");
	if(NULL == pJHCommReleaseArgStruct)
	{
		printf("Failure to load pJHCommReleaseArgStruct address!\n");
		ret = -1;
		goto ERROR_OPERATION;
	}

	pJHMountTransportLayer = (PJHMOUNTTRANSPORTLAYER)GetProcAddress(hInstZk, "JHMountTransportLayer");
	if(NULL == pJHMountTransportLayer)
	{
		printf("Failure to load JHMountTransportLayer address!\n");
		ret = -1;
		pJHCommReleaseArgStruct(pargstruct);
		goto ERROR_OPERATION;
	}

	pJHMountTransportLayer(hdl, (JHTRANSPORTLAYER *)(pjhtransportlayer), (const void *)(pargstruct));
	pJHCommReleaseArgStruct(pargstruct);

	/* Draw Text operation*/
	pJHDrawText= (PJHDRAWTEXT)GetProcAddress(hInstZk, "JHDrawText");
	if(NULL == pJHDrawText)
	{
		printf("Failure to load pJHDrawText address!\n");
		ret = -1;
		goto ERROR_OPERATION;
	}

	res = pJHDrawText(hdl, 0, 0, 32, 16, 2, "1234");
	if(JR_OK == res)
	{
		printf("\nSuccessed in drawing Text!\n");
		ret = 0;
	}
	else
	{
		printf("\nFailure to draw Text!\n");
		ret = -1;
	}

ERROR_OPERATION:
	pJHCommDeinit = (PJHCOMMDEINIT)GetProcAddress(hInstComm, "JHCommDeinit");
	pJHCommDeinit();

	/* Delete instance */
	pJHDeleteInst= (PJHDELETEINST)GetProcAddress(hInstZk, "JHDeleteInst");
	pJHDeleteInst(hdl);

	/* Free resources jinhanzk.dll */
	FreeLibrary(hInstZk);

	/* Free resources jinhancomm.dll */
	FreeLibrary(hInstComm);

	system("pause");

	return ret;
}

